<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2017/11/13
 * Time: 17:21
 */
namespace app\admin\controller;

use think\Loader;
use app\admin\model\CategoryDesc;
use app\admin\model\Upload;
use think\Request;

class Category extends Admin{
    function _initialize()
    {
        parent::_initialize();
    }
    public function index()
    {
        return view();
    }
    public function getKvData(){
        $roleData = model('Category')->getKvData();
        var_dump($roleData);
    }
    public function desc($id=0){
        if(intval($id) < 0){
            return info(lang('Data ID exception'), 0);
        }

//        $roleData = model('Category')->getKvData();
////        var_dump($roleData);
//        $this->assign('roleData', $roleData);
        $data = model('CategoryDesc')->get(['id'=>$id]);
        $category_data = model('Category')->get(['id'=>$data['category_id']]);
        $data['category_name']= $category_data['name'];
        $this->assign('data',$data);
        $this->assign('ids',$id);
        return $this->fetch();
    }
    public function upload(){
        $file = request()->file('image');
        $data = model('upload')->uplaod($file);
        return $data;
    }
    /**
     * 异步获取列表数据
     *
     * @author chengbin
     * @return mixed
     */
    public function getCategory()
    {
        if(!request()->isAjax()) {
            $this->error(lang('Request type error'), 4001);
        }

        $request = request()->param();

        $data = model('Category')->getList( $request );
        return $data;
    }
    /**
     * 添加
     */
    public function add(){
        $roleData = model('Category')->getKvData();
        $this->assign('roleData', $roleData);
        return $this->fetch('edit');
    }
    /**
     * 编辑
     * @param  string $id 数据ID（主键）
     */
    public function edit($id = 0)
    {
        if(intval($id) < 0){
            return info(lang('Data ID exception'), 0);
        }

        $roleData = model('Category')->getKvData();
        $this->assign('roleData', $roleData);
        $data = model('Category')->get(['id'=>$id]);
        $this->assign('data',$data);
        $this->assign('id',$id);
        return $this->fetch();
    }
    /**
     * 保存数据
     * @param array $data
     *
     * @author chengbin
     */
    public function saveData()
    {
        $this->mustCheckRule( 'admin/Category/edit' );
        if(!request()->isAjax()) {
            return info(lang('Request type error'));
        }

        $data = input('post.');

//        var_dump($data);die;
        return model('Category')->saveData( $data );
    }
    public function saveDesc(){

        $this->mustCheckRule( 'admin/Category/edit' );
        if(!request()->isAjax()) {
            return info(lang('Request type error'));
        }

        $data = input('post.');

//        $uploadModel = new Upload();
//        $data['image'] = $uploadModel->uplaodImg($data['c_img']);
//        $data['image'] = model('Upload')->uplaodImg($data['image']);
//        var_dump($data);die;
        $desc = new CategoryDesc();
        return $desc->saveDate( $data );
    }
    public  function uploadImg(){
        $request = $this->request;
        $post = $request->post();
        $data = input('request.');
//        $dataUplaod = model('Upload')->uplaodImg($data);
        var_dump($post);die;
        return $dataUplaod;
    }
    /**
     * 删除
     * @param  string $id 数据ID（主键）
     */
    public function delete($id = 0){
        if(empty($id)){
            return info(lang('Data ID exception'), 0);
        }
        if (intval($id == 1)) {
            return info(lang('Delete without authorization'), 0);
        }
        return Loader::model('Category')->deleteById($id);
    }
}